<?php

namespace app\speedDating\services;

use app\speedDating\interfaces\ServiceStatus;
use app\speedDating\model\SddFinanceAccount;
use app\speedDating\model\SddFinanceAccountLog;
use app\speedDating\services\common\FinanceUtilsService;

class FinanceService
{
    /**
     * 查询资产账户列表
     * @param $userID
     * @return SddFinanceAccount[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($userID)
    {
        return SddFinanceAccount::getAccountListByUserID($userID);
    }

    /**
     * 查询资产账户的所有记录
     * @param $data
     * @param $userID
     * @return \app\speedDating\model\traits\SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function logList($data, $userID)
    {
        return SddFinanceAccountLog::getAllTypeAccountLogByUserID($data['size'], $data['page'], $userID);
    }

    /**
     * 查询资产账户记录详情
     * @param $logID
     * @return \app\speedDating\model\traits\SimpleModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detailById($logID)
    {
        return SddFinanceAccountLog::getCompleteByID($logID);
    }

    /**
     * 创建资产账户
     * @param $data
     * @param int $account_type
     * @return SddFinanceAccount|array|\think\Collection
     */
    public function createAccount($data, int $account_type = ServiceStatus::ACCOUNT_TYPE_BALANCE)
    {
        return SddFinanceAccount::createAccount($data['user_id'], $account_type);
    }

    /**
     * 资产账户扣减
     * @param $id
     * @param $balance
     * @param $remark
     * @param $order
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function reduceBalance($id, $balance, $remark = null, $order = null)
    {
        if ($balance == 0) return;

        $balance = -abs($balance);

        return FinanceUtilsService::financeLogGenerate($id, $balance, $remark, $order);
    }

    /**
     * 资产账户添加
     * @param $id
     * @param $balance
     * @param $remark
     * @param $order
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addBalance($id, $balance, $remark = null, $order = null)
    {
        if ($balance == 0) return;

        $balance = abs($balance);

        return FinanceUtilsService::financeLogGenerate($id, $balance, $remark, $order);
    }

}